<?php
namespace app\admin\controller;
use cmf\controller\AdminBaseController;
use app\admin\model\DoctorModel;
use think\Request;
use think\Validate;

class DoctorController extends AdminBaseController
{
	public function index()
	{
		$doctorModel = new DoctorModel();
		$data = $doctorModel->getDataList(0);
		$this->assign([
            "data"=>$data,
            "page"=>$data->render()
        ]);
        return $this->fetch();
	}
		    /**
     * 显示创建资源表单页.
     *
     * @return \think\Response
     */
    public function create()
    {
        return $this->fetch();
    }
   /**
     * 保存新建的资源
     *
     * @param  \think\Request  $request
     * @return \think\Response
     */
    public function save(Request $request)
    {
        $data = $request->post();
        $validate = new Validate([
            "d_name"    	 =>     "require",
            "d_desc"     	 =>     "require",
            "d_head"         =>     "require",
            "d_erwei"        =>     "require",
            "d_shanchang"    =>     "require",
        ]);

        $validate->message([
            "d_name.require"     	 =>     "缺少参数：医生姓名！",
            "d_desc.require"         =>     "缺少参数：医生简介！",
            "d_head.require"         =>     "缺少参数：医生头像！",
            "d_erwei.require"        =>     "缺少参数：医生二维码！",
            "d_shanchang.require"    =>     "缺少参数：医生擅长领域！",
        ]);

        if(!$validate->check($data)){
            $this->error($validate->getError());
        }

        $model = new DoctorModel();
        if($model->doAdd($data)){
            $this->success("添加成功","Doctor/index");
        }else{
            $this->error("添加失败");
        }
    }
    //修改医生信息
    	public function edit($id)
	{
		$DocModel = new DoctorModel();
		$data = $DocModel->getDataById($id);
        $this->assign([
            "data"=>$data
        ]);
        return $this->fetch();
	}
	public function update(Request $request, $id)
	{
		 $data = $request->post();
		 $model = new DoctorModel();
		if($model->doEdit($data,$id)){
            $this->success("修改成功","Doctor/index");
        }else{
            $this->error("修改失败");
        }

	}
	    /**
     * 删除指定资源
     *
     * @param  int  $id
     * @return \think\Response
     */
    public function delete($id)
    {
        $model = new DoctorModel();
        if($model->doDel($id)){
            $this->success("删除成功");
        }else{
            $this->error('删除失败');
        }
    }
}